<template>
  <div class="wrapper">
    <div class="actions" style="margin-bottom: 10px">
      <div class="left">
        <el-button type="primary" plain size="mini" @click="handleClickAdd"
          >新增</el-button
        >
        <el-button type="primary" plain size="mini" @click="handleClickDel"
          >删除</el-button
        >
      </div>
      <div class="right">
        <el-pagination
          background
          small
          :current-page="paginations.pageIndex"
          :page-sizes="paginations.pageSizes"
          :page-size="paginations.pageSize"
          :layout="paginations.layout"
          :total="paginations.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      border
      stripe
      align="center"
      @selection-change="handleSelectionChange"
      :max-height="maxheight"
    >
      <el-table-column type="selection" width="45"> </el-table-column>
      <el-table-column
        prop=""
        fixed
        label="序号"
        width="50"
        type="index"
      ></el-table-column>
      <el-table-column
        prop="user_name"
        label="培训人"
        width="180"
      ></el-table-column>

      <el-table-column prop="job" label="职务"></el-table-column>
      <el-table-column prop="training_time" label="培训时间" width="180">
        <template slot-scope="scope">
          <span v-time="scope.row.training_time"></span>
        </template>
      </el-table-column>
      <el-table-column prop="issue_time" label="签发时间" width="180">
        <template slot-scope="scope">
          <span v-time="scope.row.issue_time"></span>
        </template>
      </el-table-column>
      <el-table-column
        prop=""
        fixed="right"
        align="center"
        label="操作"
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            plain
            size="mini"
            @click="handleOperation(scope.row, 'detail')"
            >详情
          </el-button>
          <el-button
            type="text"
            plain
            size="mini"
            @click="handleOperation(scope.row, 'edit')"
            >编辑
          </el-button>
          <el-button
            type="text"
            plain
            size="mini"
            @click="handleOperation(scope.row, 'download')"
            >下载证书
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <certificateEdit
      :mode="mode"
      :id="nowId"
      v-if="dialogVisible"
      :dialogVisible="dialogVisible"
      @close="handleCloseDialog"
    ></certificateEdit>
  </div>
</template>

<script>
import {
  certificateList,
  certificateDelete,
  certificateDownload,
} from "@/api/qms.js";
import certificateEdit from "./certificateEdit.vue";

export default {
  name: "certificateList",
  components: { certificateEdit },
  props: [],
  data() {
    return {
      maxheight: "auto",
      tableData: [],
      paginations: {
        pageIndex: 1, // 当前位于哪页
        pageSize: 20, // 1页显示多少条
        pageSizes: [10, 20, 50, 100], //每页显示多少条
        total: 200,
        layout: "total, sizes, prev, pager, next, jumper", // 翻页属性
      },
      checkedList: [],
      searchParams: {},
      dialogVisible: false,
      nowId: "",
      mode: "",
    };
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    handleSelectionChange(checked) {
      this.checkedList = checked.map((item) => item.id);
    },
    handleClickAdd() {
      this.mode = "add";
      this.dialogVisible = true;
    },
    handleOperation(row, mode) {
      if (mode === "download") {
        certificateDownload({
          id: row.id * 1,
        }).then((res) => {
          if (res.code === 10000) {
            this.$message({
              message: "下载证书成功",
              type: "success",
            });
            let filePath = res.data.data;
            filePath = filePath.replace("http:", "https:");
            window.open(filePath);
          }
        });
        return;
      }
      this.nowId = row.id;
      this.mode = mode;
      this.dialogVisible = true;
    },
    handleCloseDialog(isUpdate) {
      this.dialogVisible = false;
      isUpdate && this.getDataList();
    },
    handleClickDel() {
      if (this.checkedList.length === 0) {
        this.$message({
          message: "请选中后进行操作",
          type: "warning",
        });
        return;
      }
      certificateDelete({
        ids: this.checkedList,
      }).then((res) => {
        if (res.code === 10000) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getDataList();
        }
      });
    },
    // 每页条数改变
    handleSizeChange(val) {
      this.paginations.pageSize = val;
      this.getDataList();
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.paginations.pageIndex = val;
      this.getDataList();
    },
    getDataList() {
      certificateList({
        page: this.paginations.pageIndex,
        page_size: this.paginations.pageSize,
        ...this.searchParams,
      }).then((res) => {
        this.tableData = res.data.list || [];
        if (this.tableData.length > 6) {
          //一排操作栏
          this.maxheight = document.documentElement.clientHeight - 245;
        } else {
          this.maxheight = "auto";
        }
        this.paginations.total = res.data.pagination.total;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  padding: 10px 15px;
  background-color: #fff;

  .orderHeader {
    .orderSearch {
      form {
        ::v-deep .el-form-item__label {
          font-size: 14px;
        }

        .el-textarea {
          width: 135px;
        }
      }
    }
  }

  .actions {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    width: 100%;
    background-color: #fff;

    .el-button + .el-dropdown,
    .el-dropdown + .el-button {
      margin-left: 10px;
    }
  }
}
</style>